package com.wzy.oa_sys.controller.notice;


import com.wzy.oa_sys.model.request.notice.NoticeReq;
import com.wzy.oa_sys.model.response.notice.NoticeResp;
import com.wzy.oa_sys.service.notice.NoticeService;
import com.wzy.oa_sys.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("/list")
    public List<NoticeResp> getTaskList(HttpServletRequest request) {
        Long userId = jwtUtil.extractUserId(request);
        return noticeService.getNoticeList(userId);
    }

    @PostMapping("/addNotice")
    public void addNotice(@RequestBody NoticeReq noticeReq,HttpServletRequest request) {
        Long userId = jwtUtil.extractUserId(request);
        System.out.println("新增公告："+noticeReq.toString());
        noticeService.addNotice(noticeReq,userId);
    }

    @DeleteMapping("/deleteNoticeById/{id}")
    public void deleteNoticeById(@PathVariable("id") Integer id) {
        System.out.println("删除公告："+id);
        int i =noticeService.deleteNoticeById(id);
        System.out.println("删除结果："+i);
    }

    @GetMapping("/getNoticeDetailById/{id}")
    public NoticeResp getNoticeDetailById(@PathVariable("id") Integer id) {
        return noticeService.getNoticeDetailById(id);
    }

    @GetMapping("/getMyNoticeList")
    public List<NoticeResp> getMyNoticeList(HttpServletRequest request) {
        Long userId = jwtUtil.extractUserId(request);
        String s = jwtUtil.extractUserRole(request);
//        System.out.println("用户role："+s);
        return noticeService.getMyNoticeList(userId,s);
    }

    @GetMapping("/getSomeNotices")
    public List<NoticeResp> getSomeNotices(HttpServletRequest request) {
        Long userId = jwtUtil.extractUserId(request);
        String s = jwtUtil.extractUserRole(request);
        return noticeService.getSomeNotices(userId,s);
    }

    @GetMapping("/getICanApprovedNotices")
    public List<NoticeResp> getICanApprovedNotices(HttpServletRequest request) {
        Long userId = jwtUtil.extractUserId(request);
         return noticeService.getICanApprovedNotices(userId);
    }

    @PutMapping("/updateTheNotice")
    public void updateTheNotice(@RequestBody NoticeReq noticeReq,HttpServletRequest request) {
        noticeService.updateTheNotice(noticeReq,request);
    }

    @PostMapping("/readNotice/{id}")
    public void readNotice(@PathVariable("id") long id,HttpServletRequest request) {
        noticeService.readNotice(id,request);
    }

    @PutMapping("/updateNotice")
    public void updateNotice(@RequestBody NoticeReq noticeReq,HttpServletRequest request) {
        noticeService.updateNotice(noticeReq,request);
    }

}
